<?php
/**
 * 演示控制器
 */

namespace Admin\Controller;

use Think\Controller;
use Common\Lib\Upload;

class DemoController extends PublicController{

    private $model = 'demo';

    //列表
    public function index(){

        $model = $this->model;

        //搜索条件
        $filter = array();
        $search = array();

        //标题搜索
        $title = I('title', '', 'trim');
        if($title){
            $filter['title'] = array('like',"%$title%");
            $search['title'] = $title;
        }

        //昵称搜索
        $nick_name = I('nick_name', '', 'trim');
        if($nick_name){
            $map['nick_name'] = array('like',"%$nick_name%");
            //获取管理员id数组
            $admin_ids = M('admin')->where($map)->getField('id',true);

            $filter['admin_id'] = array('in', $admin_ids);

            $search['nick_name'] = $nick_name;
        }

        //时间区间搜索
        //$start_time = I('start_time', date('Y-m-d 00:00:00'), 'trim');
        $start_time = I('start_time', '2016-06-01 00:00:00', 'trim');
        if ($start_time) {
            $filter['add_time'] = array(array('egt', $start_time));

            $search['start_time'] = $start_time;

        }
        $end_time = I('end_time', date('Y-m-d H:i:s'), 'trim');
        if ($end_time) {
            $filter['add_time'][] = array('elt', $end_time);

            $search['end_time'] = $end_time;

        }

        //状态搜索
        $status = I('status', 0);
        if($status){
            $filter['status'] = $status;

            $search['status'] = $status;
        }

        //print_r($search);

        //获取总数据数
        $total = M($model)->where($filter)->count();
        if($total){
            //每页显示条数
            $per_num = 10;

            //调用分页函数
            $page = P($total, 5);
            //获取数据列表
            $list = M($model)->where($filter)->limit($page->limit())->order('`id` DESC')->select();
        }

        //将数据赋值到模板中
        $this->assign('page', $page);
        $this->assign('list', $list);
        $this->assign('search', $search);

        //显示模板
        $this->display('list');

    }


    //添加
    public function add(){

        $model = $this->model;

        if(!IS_POST){
            $action_url = U('demo/add');

            $this->assign('action_url', $action_url);
            $this->display('add');
        }else{
            //入库操作
            $data = I('data', array());

            //print_r($data);exit;

            extract($data);

            //验证标题是否重复
            if(!_only($model, array('title' => trim($title)))){
                $this->error('该标题已存在');
            }

            $data['add_time'] = date('Y- m-d', $add_time);


            $attachment_id = $this->upload('cover');
            if($attachment_id){
                $data['cover'] = $attachment_id;
            }

            $data['admin_id'] = \admin_helper::get_admin_id();
            $data['add_time'] = date('Y-m-d H:i:s'); 
            
            $result = M($model)->add($data);
            if($result){
                $this->success('添加成功', U('demo/index'));
            }else{
                $this->error('添加失败');
            }

        }

    }

    //编辑
    public function edit(){

        $model = $this->model;

        $id = I('id', 0);

        $info = _uri($model, $id);

        if(!$info){
            $this->error('该文章不存在');
        }


        if(!IS_POST){
            $action_url = U('demo/edit');

            $this->assign('action_url', $action_url);
            $this->assign('info', $info);

            $this->display('add');
        }else{
            //更新操作
            $data = I('data', array());

            //extract($data);
            //print_r($data);exit;



            //验证标题是否重复
            if(!_only($model, array('title' => $title), $id)){
                $this->error('该标题已存在');
            }

            //echo M($model)->_sql();exit;

            $attachment_id = $this->upload('cover');
            if($attachment_id){
                $data['cover'] = $attachment_id;
            }

            $data['id'] = $id;

            $result = M($model)->save($data);
            if(0 != $result || false != $result){
                $this->success('更新成功', U('demo/index'));
            }else{
                $this->error('更新失败');
            }

        }

    }

    //详情
    public function _empty(){
        $id = ACTION_NAME;
        //注意ctype_digit()与 is_numeric()的区别
        if(ctype_digit($id)){
            $info = _uri('demo', $id);

            $this->assign('info', $info);
            $this->display('detail');
        }else{
            $this->redirect('demo/index');
        }
    }

    //ajax删除
    public function delete(){
        $model = $this->model;

        $id = I('id', 0);

        $info = _uri($model, $id);

        if(!$info){
            $this->error('该记录不存在');
        }

        $result = M($model)->delete($id);
        if($result){
            $this->ajaxReturn('ok');
        }else{
            $this->ajaxReturn('删除失败，请稍后再试');
        }
    }

    //上传处理
    private function upload($name){
        //判断是否上传封面
        if($_FILES[$name]['name']){
            $upload = new Upload();

            $upload_info = $upload->image($name, true);

            if($upload_info['error']){
                $this->error($upload_info['message']);
            }
            $attachment_id = $upload_info['attachment_id'];
        }

        return $attachment_id;

    }

}